package com.example.demo.Control;

import com.example.demo.Common.AjaxResult;
import com.example.demo.Common.PublicValue;
import com.example.demo.Common.StringTools;
import com.example.demo.Common.UserSessionTools;
import com.example.demo.Entity.Article;
import com.example.demo.Entity.ArticleVo;
import com.example.demo.Entity.Userinfo;
import com.example.demo.Service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/art")
public class ArticleControl {
    @Autowired
   public  ArticleService articleService;
    //添加文章
    @RequestMapping("/add")
    public AjaxResult add(Article article, HttpServletRequest req) {
        //进行非空判断
        // 1.非空效验
        if (article == null ||
                !StringUtils.hasLength(article.getTitle()) ||
                !StringUtils.hasLength(article.getContent()))
            return AjaxResult.fail(-1, "参数异常");
        HttpSession session= req.getSession();
        Userinfo userinfo=(Userinfo) session.getAttribute(PublicValue.SessionValue);
        System.out.println(userinfo.getId());
        article.setUid(userinfo.getId());
        int result=articleService.add(article);
        return AjaxResult.success(result);
    }
    //根据文章id查询文章
    @RequestMapping("/getDetailById")
    public  AjaxResult selectById(Integer id,HttpServletRequest req){
        if(id==null||id<=0){
            return AjaxResult.fail(-1,"参数有误");
        }
        Userinfo userinfo= UserSessionTools.getLoginUser(req);
        return AjaxResult.success(articleService.getDetailById(id, userinfo.getId()));
    }
    //修改文章
    @RequestMapping("/update")
    public  AjaxResult update(Article article,HttpServletRequest req){
       //非空判断
        if(null==article||!StringUtils.hasLength(article.getTitle())
           ||!StringUtils.hasLength(article.getContent())){
            return AjaxResult.fail(-1,"参数错误");
        }
        Userinfo userinfo=UserSessionTools.getLoginUser(req);
        article.setUid(userinfo.getId());
        //修改更改时间
        article.setUpdatetime(LocalDateTime.now());
        int result=articleService.update(article);
        return AjaxResult.success(result);
    }
    //获取文章
    @RequestMapping("/getDetail")
    public AjaxResult getDetail(Integer id){
        if(id==null||id<=0){
            return AjaxResult.fail(-1,"参数有误");
        }
       ArticleVo articlevo= articleService.getDetail(id);
        return AjaxResult.success(articlevo);
    }
    //增加浏览数
    @RequestMapping("/addRcount")
    public AjaxResult addRcount(Integer id){
        if (id==null||id<=0){
            return AjaxResult.fail(-1,"参数有误");
        }
        int result=articleService.addRcount(id);
        return AjaxResult.success(result);
    }
    //统计个人文章数
    @RequestMapping("/countArt")
    public  AjaxResult countArt(HttpServletRequest req){
        Userinfo userinfo=UserSessionTools.getLoginUser(req);
        int count=articleService.countArt(userinfo.getId());
        return AjaxResult.success(count);
    }
    //获取myblog_list的文章信息
    @RequestMapping("/getMyArtList")
    public AjaxResult getMyArtList(HttpServletRequest req){
        Userinfo userinfo=UserSessionTools.getLoginUser(req);
        List<Article> list = articleService.getMyArtList(userinfo.getId());
       //设置文章摘要
        for (Article item:list) {
            String content= StringTools.StringSub(item.getContent() ,PublicValue.maxLength);
            //将文章正文中markdown关键字去掉
            System.out.println(content.length());
            content=content.replace(String.valueOf('#'), "");
            item.setContent(content);

        }
        return AjaxResult.success(list);
    }
    //删除文章
    @RequestMapping("/delById")
    public AjaxResult del(Integer id,HttpServletRequest req){
        if(id==null||id<=0){
            return  AjaxResult.fail(-1,"参数有误");
        }
        Userinfo userinfo=UserSessionTools.getLoginUser(req);
        int result=articleService.delById(id, userinfo.getId());
        return AjaxResult.success(result);
    }
    //文章分页

    /**
     *
     * @param pageIndex 页数
     * @param pageSize  //每页最大展示量
     * @return
     */
    @RequestMapping("/pageLimt")
    public  AjaxResult pageLimt(Integer pageSize,Integer pageIndex){
        //默认处理
        //pageIndex ：当前页码
        if(pageIndex==null||pageIndex<=0){
            pageIndex=1;
        }
        //pageSize :每页的最大条数
        if(pageSize==null||pageSize<=0){
            pageSize=2;
        }
        int offset=(pageIndex-1)*pageSize;
        List<Article> list= articleService.pageLimt(pageSize,offset);
        list.stream().forEach(item -> {
            item.setContent(StringTools.StringSub(item.getContent(),PublicValue.maxLength));

        });
        return AjaxResult.success(list);
    }
    @RequestMapping("/sumArt")
    public  AjaxResult sumArt(HttpServletRequest req){
        Userinfo userinfo=UserSessionTools.getLoginUser(req);
        int count=articleService.sumArt();
        return AjaxResult.success(count);
    }
}
